﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using SMARTMobile.Services.SMARTMobileServiceReference;
using System.ServiceModel;
using Microsoft.Phone.Info;
using SMARTMobile.Utilities;

namespace SMARTMobile.Views
{
    public partial class HoTroPage
    {
        PhotoChooserTask selectphoto = null;
        private ReloadDataSmartPhoneSoapClient _proxy;
        private BasicHttpBinding _binding;
        private BitmapImage image;
        private string imageFileName;

        public HoTroPage()
        {
            InitializeComponent();
            _binding = new BasicHttpBinding();
            _binding.MaxBufferSize = 2147483647;
            _binding.MaxReceivedMessageSize = 2147483647;
            _binding.SendTimeout = new TimeSpan(0, 0, 10, 0);
            _binding.ReceiveTimeout = new TimeSpan(0, 0, 10, 0);
            _binding.OpenTimeout = new TimeSpan(0, 0, 10, 0);
            _binding.CloseTimeout = new TimeSpan(0, 0, 10, 0);
            this._proxy = new ReloadDataSmartPhoneSoapClient(_binding, new System.ServiceModel.EndpointAddress("http://wsnokia.smartbs.vn/ReloadDataSmartPhone.asmx"));
            this._proxy.PostLoiCompleted += _proxy_PostLoiCompleted;
        }

        private void ImageError_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.selectphoto = new PhotoChooserTask();
            this.selectphoto.Completed += selectphoto_Completed;
            this.selectphoto.ShowCamera = true;
            this.selectphoto.Show();
        }

        private void selectphoto_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
                imageFileName = e.OriginalFileName;
                this.ImageError.Source = image;
                imageFileName = ImageUtilities.CreateImageName();
                if (!string.IsNullOrEmpty(imageFileName))
                {
                    ImageUtilities.SaveToJpeg(image, imageFileName);
                }
            }
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Terminate();
        }

        private void GuiLoi_Clicked(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.BodyTextBox.Text))
            {
                MessageBox.Show("Bạn chưa nhập nội dung lỗi, không thể gửi.");
                return;
            }

            if (string.IsNullOrWhiteSpace(imageFileName))
            {
                MessageBox.Show("Bạn chưa chụp hình, không thể gửi.");
                return;
            }

            byte[] byteData = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
            string imei = System.Convert.ToBase64String(byteData);
            string idDevice = string.IsNullOrEmpty(imei) ? string.Empty : imei;
            string bitmapString = ImageUtilities.ConvertBitmapToString(imageFileName);
            this._proxy.PostLoiAsync(imei, imei, this.BodyTextBox.Text, imei, bitmapString);
        }

        void _proxy_PostLoiCompleted(object sender, PostLoiCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Gửi lổi thành công.");
                Application.Current.Terminate();
            }
            else
            {
                MessageBox.Show("Gửi lổi thất bại. Vui lòng kiểm tra lại.");
            }
        }
    }
}